#!/bin/bash
set -e

setup_etc()
{
    mkdir -p /etc /proc
    mount -t tmpfs none /etc
    mount -t proc none /proc
    cp -rfp /usr/etc/* /etc
}

setup_modules()
{
    if [ -e .base/lib/modules/$(uname -r) ]; then
        mount --bind .base/lib/modules /lib/modules
    fi
    if [ -e .base/lib/firmware ]; then
        mount --bind .base/lib/firmware /lib/firmware
    fi
}

setup_users()
{
    sed -i 's!/bin/ash!/bin/bash!' /etc/passwd
    mkdir -p /home
    addgroup -S sudo
    sed -i 's/^(sudo:.*)/\1rancher/g' /etc/group
    addgroup -g 1000 rancher
    adduser -s /bin/bash -u 1000 -D -G rancher rancher
    echo 'rancher:*' | chpasswd -e
}

setup_dirs()
{
    mkdir -p /run/k3os
}

setup_config()
{
    if [ "$K3OS_MODE" = "local" ]; then
        return 0
    fi

    $K3OS_SYSTEM/k3os/current/k3os config --initrd
}

setup_etc

. $SCRIPTS/functions

setup_modules
setup_users
$K3OS_SYSTEM/k3os/current/k3os rc
setup_dirs
setup_kernel
setup_config

pinfo Bootstrapped
